package com.ofps.web.controller.flow;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ofps.common.annotation.Log;
import com.ofps.common.core.controller.BaseController;
import com.ofps.common.core.domain.AjaxResult;
import com.ofps.common.enums.BusinessType;
import com.ofps.flow.domain.EnterFlowNode;
import com.ofps.flow.service.IEnterFlowNodeService;
import com.ofps.common.utils.poi.ExcelUtil;
import com.ofps.common.core.page.TableDataInfo;

/**
 * 流程节点Controller
 * flow_node：某个流程的节点信息（如果一个流程有4个节点，则对应应该有四项）
 * ● id：唯一标识（不太重要）
 * ● name：节点的名称
 * ● flow_id：该节点对应的流程id
 * ● order：在该流程内，节点的次序
 * ● last：该节点是否为最后一个节点
 * ● related_user_id：审核该节点的用户id
 *
 * @author ofps
 * @date 2022-06-01
 */
@RestController
@RequestMapping("/flowNode")
public class EnterFlowNodeController extends BaseController {
    @Autowired
    private IEnterFlowNodeService enterFlowNodeService;

    /**
     * 查询流程节点列表
     */
    @PreAuthorize("@ss.hasPermi('flowNode:list')")
    @GetMapping("/list")
    public TableDataInfo list(EnterFlowNode enterFlowNode) {
        startPage();
        List<EnterFlowNode> list = enterFlowNodeService.selectEnterFlowNodeList(enterFlowNode);
        return getDataTable(list);
    }

    /**
     * 导出流程节点列表
     */
    @PreAuthorize("@ss.hasPermi('flowNode:export')")
    @Log(title = "流程节点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnterFlowNode enterFlowNode) {
        List<EnterFlowNode> list = enterFlowNodeService.selectEnterFlowNodeList(enterFlowNode);
        ExcelUtil<EnterFlowNode> util = new ExcelUtil<EnterFlowNode>(EnterFlowNode.class);
        util.exportExcel(response, list, "流程节点数据");
    }

    /**
     * 获取流程节点详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowNode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(enterFlowNodeService.getById(id));
    }

    /**
     * 新增流程节点
     */
    @PreAuthorize("@ss.hasPermi('flowNode:add')")
    @Log(title = "流程节点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnterFlowNode enterFlowNode) {
        return toAjax(enterFlowNodeService.save(enterFlowNode));
    }

    /**
     * 修改流程节点
     */
    @PreAuthorize("@ss.hasPermi('flowNode:edit')")
    @Log(title = "流程节点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnterFlowNode enterFlowNode) {
        return toAjax(enterFlowNodeService.updateById(enterFlowNode));
    }

    /**
     * 删除流程节点
     */
    @PreAuthorize("@ss.hasPermi('flowNode:remove')")
    @Log(title = "流程节点", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(enterFlowNodeService.removeByIds(Arrays.asList(ids)));
    }
}
